package array;

public class LongestCommonPrefix {

    public String longestCommonPrefix(String[] strs) {

        int m = strs.length;
        // 以第一行列数为基准
        int n = strs[0].length();
        for (int col = 0; col < n; col++) {
            for (int row = 1; row < m; row++) {
                String cur = strs[row], pre = strs[row - 1];
                // 判断每个字符的col索引是否相等
                if (col >= cur.length() || col >= pre.length() ||
                cur.charAt(col) != pre.charAt(col)) {
                    // 发现不匹配的字符，只有strs[row][0...col - 1]是公共前缀
                    return strs[row].substring(0, col);
                }
            }
        }
        return strs[0];
    }
}
